<template>
  <el-dialog
    :close-on-click-modal="false"
    :title="isEdit ? '修改' : '新增'"
    width="800px"
    :visible.sync="visible"
  >
    <el-form :model="form" ref="form" :label-position="labelPosition" label-width="100px">
      <el-row :gutter="24">
        <el-col>
          <el-form-item label="任职起始时间">
            <el-date-picker
              type="date"
              :style="inputWidth"
              format="yyyy-MM-dd"
              value-format="yyyy-MM-dd"
              v-model="form.onboardingDate"
              placeholder="请选择"
            />
          </el-form-item>
        </el-col>
        <el-col>
          <el-form-item label="任职终止时间">
            <el-date-picker
              type="date"
              :style="inputWidth"
              format="yyyy-MM-dd"
              value-format="yyyy-MM-dd"
              v-model="form.endDate"
              placeholder="请选择"
            />
          </el-form-item>
        </el-col>
        <el-col>
          <el-form-item label="科室">
            <dept-select
              :selectValue="form.deptId"
              ref="deptSelect"
              :style="inputWidth"
              @select="e => handleDeptSelect(e, 'dept')"
            />
          </el-form-item>
        </el-col>
        <el-col>
          <el-form-item label="岗位">
            <dept-position-s
              :style="inputWidth"
              :selectValue="form.positionId"
              ref="deptPositionS"
              @select="e => handleDeptSelect(e, 'position')"
            />
          </el-form-item>
        </el-col>
        <el-col>
          <el-form-item label="人员类别">
            <el-select clearable :style="inputWidth" v-model="form.personType" placeholder="请选择">
              <el-option
                v-for="(item,index) in typeArr"
                :key="index"
                :label="item.title"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col>
          <el-form-item label="职务">
            <el-select clearable :style="inputWidth" v-model="form.duty" placeholder="请选择">
              <el-option
                v-for="(item,index) in dutyArr"
                :key="index"
                :label="item.title"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col>
          <el-form-item label="备注">
            <el-input v-model="form.remark" :style="inputWidth" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button @click="handleUpdate" type="primary">{{ isEdit ? '修改' : '新增'}}</el-button>
    </div>
  </el-dialog>
</template>

<script>
import DeptSelect from '@/components/DeptSelect'
import DeptPositionS from '@/components/DeptPositionS'
import { updateUserOffice, createUserOffice } from '@/api/archives'
import { openNotification, getDicData, getSession } from '@/utils/util'

export default {
  components: {
    DeptSelect,
    DeptPositionS
  },
  data() {
    return {
      visible: false,
      form: {},
      labelPosition: 'right',
      inputWidth: {
        width: '100%'
      },
      isEdit: false,
      userRow: null,
      typeArr: [],
      dutyArr: []
    }
  },
  methods: {
    handleUpdate() {
      this.$refs.form.validate(async valid => {
        if (valid) {
          let res = null,
            mes = null
          this.$set(this.form, 'userId', this.userRow.userId)
          if (this.isEdit) {
            res = await updateUserOffice(this.form)
            mes = '修改操作成功'
          } else {
            res = await createUserOffice(this.form)
            mes = '新增操作成功'
          }
          if (res.state === '000000') {
            openNotification('success', mes)
            this.visible = false
            this.$emit('refresh')
          }
        }
      })
    },
    edit(bool, row) {
      console.log('row', row)
      this.visible = true
      this.isEdit = bool
      this.form = Object.assign({}, row)
      if (bool) {
        this.$set(this.form, 'positionId', row.deptId + '-' + row.positionId)
      }
    },
    handleDeptSelect(value, type) {
      if (type === 'dept') {
        this.$set(this.form, 'deptId', value.key)
      } else {
        this.$set(this.form, 'positionId', value.key)
      }
    },
    handleShowHistory() {
      this.$emit('showHistory', 'office')
    }
  },
  mounted() {
    if (this.$route.query.userRow) {
      this.userRow = JSON.parse(this.$route.query.userRow)
    } else {
      this.userRow = getSession('userRow')
    }
    this.typeArr = getDicData('001', '001010')
    this.dutyArr = getDicData('001', '001008')
  }
}
</script>
